package com.cardbaobao.cardbabyclient.ui.pop;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.base.Constant;
import com.cardbaobao.cardbabyclient.utils.DensityUtil;
import com.cardbaobao.cardbabyclient.utils.ScreenUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jiangzx on 16:31.
 * 贷款底部筛选排序popupWindow
 */

public class LoanSortPop extends PopupWindow {
    private LoanSortEnsuredListener loanSortEnsuredListener;
    public void setLoanSortEnsuredListener(LoanSortEnsuredListener listener){
        this.loanSortEnsuredListener = listener;
    }
    public interface LoanSortEnsuredListener {
        void onLoanSortEnsured(String sort);
    }
    private Context context;
    private TextView tvReset;
    private TextView tvEnsure;
    private LoanSortAdapter adapter;
    private List<Bean> data;

    public LoanSortPop(Context context){
        this.context=context;
        View rootView= LayoutInflater.from(context).inflate(R.layout.pop_loan_sort,null);
        rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        View contentView=rootView.findViewById(R.id.pop_content);
        contentView.getLayoutParams().height=
                (int) (ScreenUtil.getScreenHeight(context)*0.5f);
        tvReset= (TextView) rootView.findViewById(R.id.tv_reset);
        tvEnsure= (TextView) rootView.findViewById(R.id.tv_ensure);
        RecyclerView recyclerView= (RecyclerView) rootView.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(context,
                        LinearLayoutManager.VERTICAL,false));
        data=new ArrayList<>();
        data.add(new Bean(Constant.LOAN_LIMIT,"额度由高到低"));
        data.add(new Bean(Constant.LOAN_RATE,"费率由低到高"));
        data.add(new Bean(Constant.LOAN_SPEED,"速度由快到慢"));
        data.add(new Bean(Constant.LOAN_APPR,"批核由快到慢"));
        adapter=new LoanSortAdapter();
        recyclerView.setAdapter(adapter);
        initListener();
        setWidth(ScreenUtil.getScreenWidth(context));
        //高度为屏幕高度的一半
        setBackgroundDrawable(new ColorDrawable(0));//popup全透明
        setAnimationStyle(R.style.CustomBottomAnimateStyle);
        setContentView(rootView);
    }

    private void initListener(){
        tvReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.setSelected(-1);
            }
        });
        tvEnsure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if(loanSortEnsuredListener !=null){
                    Bean bean=adapter.getSelected();
                    loanSortEnsuredListener.onLoanSortEnsured(bean==null?null:bean.code);
                }
            }
        });
    }

    public void setPopCurrent(final String code){
        if(adapter!=null){
            int selected=-1;
            for (int i=0,size=data.size();i<size;i++){
                if(data.get(i).code.equals(code)){
                    selected=i;
                    break;
                }
            }
            adapter.setSelected(selected);
        }
    }

    private class Bean{
        private String code;
        private String name;
        private Bean(String code,String name){
            this.code=code;
            this.name=name;
        }
    }

    private class LoanSortAdapter extends RecyclerView.Adapter{
        private int selected=-1;
        private static final int colorNormal=0xff222222;
        private static final int colorSelected=0xffff9e27;
        private Drawable ascNormal;
        private Drawable ascSelected;
        private Drawable descNormal;
        private Drawable descSelected;

        private LoanSortAdapter(){
            ascNormal= ContextCompat.getDrawable(context,R.mipmap.icon_sort_asc);
            ascSelected=ContextCompat.getDrawable(context,R.mipmap.icon_sort_asc_high_light);
            descNormal=ContextCompat.getDrawable(context,R.mipmap.icon_desc_normal);
            descSelected=ContextCompat.getDrawable(context,R.mipmap.icon_sort_desc_high_light);
        }
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            TextView textView=new TextView(context);
            textView.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            int drawablePadding=DensityUtil.dp2px(context,5);
            textView.setTextColor(colorNormal);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,15.0f);
            textView.setCompoundDrawablePadding(drawablePadding);
            textView.setGravity(Gravity.CENTER);
            //左、右、底部间距30
            int margin=DensityUtil.dp2px(context,15);
            //高度80
            int sizeHeight= DensityUtil.dp2px(context,40);
            RecyclerView.LayoutParams params=
                    new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT,
                            sizeHeight);
            LinearLayout ll=new LinearLayout(context);
            ll.setLayoutParams(params);
            params.leftMargin=margin;
            params.rightMargin=margin;
            params.bottomMargin=margin;
            ll.setLayoutParams(params);
            ll.setGravity(Gravity.CENTER);
            ll.addView(textView);
            return new ContentHolder(ll);
        }

        @Override
        public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
            final TextView textView= ((ContentHolder) holder).textView;
            textView.setText(data.get(position).name);
            if(position==selected){
                textView.setTextColor(colorSelected);
                holder.itemView.setBackgroundResource(R.drawable.shape_corner_20dp_solid_fff_storke_ff9e27);
            }else{
                textView.setTextColor(colorNormal);
                holder.itemView.setBackgroundResource(R.drawable.shape_corner_20dp_solid_fff_storke_eee);
            }
            switch (position){
                //降序
                case 0:
                case 2:
                case 3:
                    if(position==selected){
                        textView.setCompoundDrawablesWithIntrinsicBounds(null,null,descSelected,null);
                    }else{
                        textView.setCompoundDrawablesWithIntrinsicBounds(null,null,descNormal,null);
                    }
                    break;
                case 1:
                    if(position==selected){
                        textView.setCompoundDrawablesWithIntrinsicBounds(null,null,ascSelected,null);
                    }else{
                        textView.setCompoundDrawablesWithIntrinsicBounds(null,null,ascNormal,null);
                    }
                    break;
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setSelected(holder.getAdapterPosition());
                }
            });

        }

        private void setSelected(int selected){
            this.selected=selected;
            notifyDataSetChanged();
        }

        private Bean getSelected(){
            return selected==-1?null:data.get(selected);
        }

        @Override
        public int getItemCount() {
            return data.size();
        }

        private class ContentHolder extends RecyclerView.ViewHolder{
            private TextView textView;
            private ContentHolder(View itemView) {
                super(itemView);
                textView= (TextView) ((LinearLayout)itemView).getChildAt(0);
            }
        }
    }

}
